﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;

public partial class Users_CreateUser : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!Page.IsPostBack)
        {
              CheckBoxList ckblRoles =
              twsRoleStep.ContentTemplateContainer.FindControl("ckblRoles") as CheckBoxList;
              if (ckblRoles != null)
              {
                  ckblRoles.DataSource = Roles.GetAllRoles();
                  ckblRoles.DataBind();
              }
            
        }
    }
    protected void cuwNewUser_CreatedUser(object sender, EventArgs e)
    {
        
    }
    
    protected void cuwNewUser_NextButtonClick(object sender, WizardNavigationEventArgs e)
    {
        switch (e.NextStepIndex)
        {
            case 2:
                AcceptRoleInfo();
                break;
            case 3:
                AcceptProfileInfo();
                break;
        }
    }

    protected void AcceptProfileInfo()
    {
        Control currentContainer = twsProfileInfo.ContentTemplateContainer;
        string firstName = GetTextValueFromTemplate(currentContainer, "tbFirstName");
        string middleName = GetTextValueFromTemplate(currentContainer, "tbMiddleName");
        string family = GetTextValueFromTemplate(currentContainer, "tbFamily");
        string ageString = GetTextValueFromTemplate(currentContainer, "tbAge");
        string address = GetTextValueFromTemplate(currentContainer, "tbAddress");
        string phone = GetTextValueFromTemplate(currentContainer, "tbPhone");
        string mobilePhone = GetTextValueFromTemplate(currentContainer, "tbMobilePhone");
        DropDownList ddlSex = currentContainer.FindControl("ddlSex") as DropDownList;
        string sex = "Male";
        if(ddlSex != null)
        {
            sex = ddlSex.SelectedValue;
        }

        string userName = cuwNewUser.UserName;
        ProfileCommon userProfile = Profile.GetProfile(userName);
        userProfile.FirstName = firstName;
        userProfile.MiddleName = middleName;
        userProfile.LastName = family;
        userProfile.Phone = phone;
        userProfile.MobilePhone = mobilePhone;
        userProfile.Address = address;
        userProfile.Sex = sex;
        int age;
        if (int.TryParse(ageString, out age))
        {
            userProfile.Age = age.ToString();
        }
        userProfile.Save();
        MembershipUser user = Membership.GetUser(userName);
        user.IsApproved = true;
        Membership.UpdateUser(user);
    }

    private string GetTextValueFromTemplate(Control template, string controlName)
    {
        string textValue = string.Empty;
        TextBox tbText = template.FindControl(controlName) as TextBox;
        if (tbText != null)
        {
            textValue = tbText.Text;
        }
        return textValue;
    }
    protected void AcceptRoleInfo()
    {
        CheckBoxList ckblRoles = 
            twsRoleStep.ContentTemplateContainer.FindControl("ckblRoles") as CheckBoxList;
        if (ckblRoles != null)
        {
            MembershipUser user = Membership.GetUser(cuwNewUser.UserName);
            if (user != null)
            {
                foreach (ListItem item in ckblRoles.Items)
                {
                    if (item.Selected)
                    {
                        if (!Roles.IsUserInRole(item.Text))
                        {
                            Roles.AddUserToRole(user.UserName, item.Text);
                        }
                    }
                }
            }
        }
    }
}
